#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"
#include "unittests.h"

int run_tests3() {
    int player1 = 0;
    int handCount = 5;
    int coppers[MAX_HAND];
    for (int i = 0; i < MAX_HAND; i++) {
        coppers[i] = copper;
    }

    struct gameState state;
    memset(&state, 0, sizeof(struct gameState)); // clear the game state
    int kingdom_cards[10] = {adventurer, council_room, feast, gardens, mine,
        remodel, smithy, village, baron, great_hall};

    // reset
    initializeGame(2, kingdom_cards, 1, &state); // initialize a new game
    state.handCount[player1] = handCount; // set the number of cards on hand
    memcpy(state.hand[player1], coppers, sizeof(int) * handCount); // set all the cards to copper

    printf("Testing discardCard()\n");
    int handPos = 0;
    int discardedCard = state.hand[player1][handPos];
    int lastCard = state.hand[player1][state.handCount[player1] - 1];

    // discard but dont trash
    assert(discardCard(handPos, player1, &state, 0) == 0);
    // Make sure that the card we discarded is the
    assert(state.playedCardCount == 1);
    // Check that the discarded card is in the playedCards pile
    assert(state.playedCards[0] == discardedCard);
    int newHandCount = state.handCount[player1];
    assert(newHandCount == 4);
    // check the that location in our hand is reset to -1
    assert(state.hand[player1][newHandCount] == -1);
    // Check that the last card is now where the discarded card was
    assert(state.hand[player1][handPos] == lastCard);

    printf("discardCard() with a 5 card hand passed\n");

    // reset
    memset(&state, 0, sizeof(struct gameState)); // clear the game state
    initializeGame(2, kingdom_cards, 1, &state); // initialize a new game
    state.handCount[player1] = 1; // set the number of cards on hand
    for (int i = 1; i < 5; i++) {
        state.hand[player1][i] = -1;
    }
    state.hand[player1][0] = copper;

    handPos = 0;
    discardedCard = state.hand[player1][handPos];

    assert(discardCard(handPos, player1, &state, 0) == 0);
    assert(state.playedCardCount == 1);
    assert(state.playedCards[0] == discardedCard);
    assert(state.handCount[player1] == 0);
    // Check all cards in hand are unset
    for (int i = 1; i < 5; i++) {
        assert(state.hand[player1][i] == -1);
    }

    printf("discardCard() with a 1 card hand passed\n");

    // reset
    /*

    the case of an empty hand and having to discard isn't properly implemented

    memset(&state, 0, sizeof(struct gameState)); // clear the game state
    initializeGame(2, kingdom_cards, 1, &state); // initialize a new game
    state.handCount[player1] = 0; // set the number of cards on hand
    for (int i = 1; i < 5; i++) {
        state.hand[player1][i] = -1;
    }

    handPos = 0;
    assert(discardCard(handPos, player1, &state, 0) == 0);
    assert(state.playedCardCount == 0);

    printf("discardCard() with a 0 card hand passed\n");
    */

    printf("\n");

    return 0;
}
